package com.jinlejia.bbs.ui.mine;
import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.SortAdapter;
import com.jinlejia.bbs.bean.base.BBSUserBean;
import com.jinlejia.bbs.databinding.BbsActivitySearchanswerBinding;
import com.jinlejia.bbs.http.utils.ContactsUtils;
import com.jinlejia.bbs.http.utils.PinyinComparator;
import com.jinlejia.bbs.ui.fun.BBSInvitedViewListActivity;
import com.jinlejia.bbs.widget.SideBar;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.bean.Clients;
import com.jinlejia.erp.utils.DbUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author SongJiaTai
 * @description:搜索类
 * @date :2019/5/27 17.50.04
 */
public class BBSSearchAnswerActiviy extends AppCompatActivity {
    private BbsActivitySearchanswerBinding binding;

    /**
     * 根据拼音来排列ListView里面的数据类
     */
    private PinyinComparator pinyinComparator;

    private SortAdapter adapter;
    private List<Clients> SourceDateList = new ArrayList<>();
    private String phoneNO;
    private String topicCode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.bbs_activity_searchanswer);
        binding.setClickcancel(this);
        topicCode = getIntent().getStringExtra("topicCode");
        binding.clientEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                View view = getWindow().peekDecorView();
                if (view != null) {
                    InputMethodManager inputmanger = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputmanger.hideSoftInputFromWindow(view.getWindowToken(), 0);
                }
                return false;
            }
        });
        initView();
        initEvent();
    }

    private void initEvent() {
        binding.idSideBar.setOnTouchingLetterChangedListener(new SideBar.OnTouchingLetterChangedListener() {
            @Override
            public void onTouchingLetterChanged(String s) {
                //该字母首次出现的位置
                if (adapter != null) {
                    int position = adapter.getPositionForSection(s.charAt(0));
                    if (position != -1) {
                        binding.idListView.setSelection(position);
                    }
                }
            }
        });

        binding.clientEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (TextUtils.isEmpty(binding.clientEdit.getText().toString())) {
                    adapter = new SortAdapter(BBSSearchAnswerActiviy.this, SourceDateList);
                    binding.idListView.setAdapter(adapter);
                } else {
                    filterData(binding.clientEdit.getText().toString());
                }
            }
        });


        binding.idListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Clients item = (Clients) adapter.getItem(i);
                BBSUserBean bbsUserBean = new BBSUserBean();
                bbsUserBean.setUserName(item.getName());
                bbsUserBean.setUserAvatar(ApiConfig.MAGEHEADER_URL().concat(item.getImageUrl()));
                bbsUserBean.setUserAddress(item.getDeptName());
                bbsUserBean.setUserPin(item.getPin());

                Intent intent = new Intent();
                intent.putExtra("topicCode",topicCode);
                intent.putExtra("user",bbsUserBean);
                intent.setClass(BBSSearchAnswerActiviy.this,BBSInvitedViewListActivity.class);
                startActivity(intent);
            }
        });

    }

    private void initView() {
        binding.idListView.setEmptyView(binding.idEmpty);
        SourceDateList = DbUtil.getDB(this).query(Clients.class);
        pinyinComparator = new PinyinComparator();
        // 根据a-z进行排序源数据
        Collections.sort(SourceDateList, pinyinComparator);
        adapter = new SortAdapter(BBSSearchAnswerActiviy.this, SourceDateList);
        binding.idListView.setAdapter(adapter);

        getData();
    }


    //返回图标点击事件
    public void bcakOnclick(View view) {
        finish();
    }

    public void onCancelClick(View view) {
        binding.clientEdit.setText("");
    }

    /**
     * 获取数据
     */
    public void getData() {

        SourceDateList = ContactsUtils.getInstance(BBSSearchAnswerActiviy.this).getContactList();
        // 根据a-z进行排序源数据
        Collections.sort(SourceDateList, pinyinComparator);
        adapter = new SortAdapter(BBSSearchAnswerActiviy.this, SourceDateList);
        binding.idListView.setAdapter(adapter);
    }


    /**
     * 根据输入框中的值来过滤数据并更新ListView
     *
     * @param filterStr
     */
    private void filterData(String filterStr) {
        List<Clients> filterDateList = new ArrayList<>();

        if (TextUtils.isEmpty(filterStr)) {
            filterDateList = SourceDateList;
        } else {
            filterDateList.clear();
            for (Clients clients : SourceDateList) {
                String name = clients.getName();
                if (name.indexOf(filterStr.toString()) != -1 || clients.getPin().toUpperCase().indexOf(filterStr.toString().toUpperCase()) != -1) {
                    filterDateList.add(clients);
                }
            }
        }
        if (adapter != null) {
            // 根据a-z进行排序
            Collections.sort(filterDateList, pinyinComparator);
            adapter.updateListView(filterDateList, filterStr);
        }
    }
}
